package screen;

import java.util.Vector;

import javax.microedition.lcdui.Graphics;

import font.BitmapFont;

import main.GameCanvas;
import model.Command;

public class ChatTab {
	
	public boolean isColor;
	public String title;
	public Command center;
	public Command right;
	Vector texts = new Vector();
	public String currentChatText = "";
	public int opponentID;
	public boolean showChatTf;
	public int dis;
	public int l=13;
	public int limit=0;	
	public static int cmtoY, cmy, cmdy, cmvy, cmyLim;

	public ChatTab(String title, int opponentID, Command center, Command right, boolean showChatTf) {
		this.title = title;
		this.opponentID = opponentID;
		this.center = center;
		this.right = right;
		this.showChatTf = showChatTf;		
		dis=GameCanvas.h - MsgScreen.Y * 2 - 54;
		reset();
		isColor=true;
	}

	public void addText(String fromName,String msg) {
		isColor=true;
		addText(fromName + ": " + msg);
	}

	public void addText(String text) {
		String[] newTexts = BitmapFont.m_bmNormalFont.splitFontBStrInLine(text, GameCanvas.w - (MsgScreen.X * 2 + 30));
	
		int a=newTexts.length;
		for (int i = 0; i < a; i++) {
			texts.addElement(newTexts[i]);
			if (texts.size() > 100)
				texts.removeElementAt(0);
		}	
		if(MsgScreen.gI().getTab(MsgScreen.gI().selectedTab)==this)
			initScroll();			
			
	}
	public void initScroll(){
		int a = texts.size();
		Scroll.gI().init(dis, a*13, cmy);		
		limit = a * l - dis;
		if(a * l > dis){
			cmy = a * l - (dis / l) * l;						
		}	
	}
	
	public void paint(Graphics g) {	
		if(limit>0){			
			Scroll.gI().paintScroll(g, GameCanvas.w-48, 0);
		}
		g.setClip(0, 0, GameCanvas.w - MsgScreen.X * 2, GameCanvas.h - MsgScreen.Y * 2-26);	
		g.translate(0, -cmy);
		int a=cmy/l;
		if(a<0)
			a=0;
		
		dis = GameCanvas.h - MsgScreen.Y * 2 - 54;
		int b=a+dis/l+1;
		if(b>texts.size())
			b=texts.size();
		for (int i = a; i < b; i++) {
			String s = (String) texts.elementAt(i);
			BitmapFont.drawNormalFont(g, s, 10, i * l+5, 0xffffff, 0);
		}
		
	}	
	public void update(int dir){
		if(dir==-1&&cmtoY>l){
			cmtoY-=l;
		}
		if(dir==1&&limit>0&&cmtoY<limit){
			cmtoY+=l;
		}
		if (cmy != cmtoY) {
			cmvy = (cmtoY - cmy) << 2;
			cmdy += cmvy;
			cmy += cmdy >> 4;
			cmdy = cmdy & 0xf;
			Scroll.gI().updateScroll(cmy, cmy+1);	
		}
	}

	public void reset() {
		cmtoY=cmy=0;
		initScroll();
		cmtoY=cmy;
	}	

}
